#include "Tuple.h"
#include "SQLInterface.h"
#include "../Tools/linked_list.h"
#include <stdlib.h>
#include <stdio.h> // can probably remove later


tuple_t* initTuple(TableID table)
{
    tuple_t* tuple = (tuple_t*)malloc(sizeof(tuple_t));
    int i;

    tuple->table = table;
    tuple->params = (void**)malloc(sizeof(char**) * getNumParams(table));
    for(i = 0; i < getNumParams(table); i++)
    {
        tuple->params[i] = NULL;
    }

    return tuple;
}


void freeTuple(tuple_t* tuple)
{
    int i;
    
    if(tuple->params)
    {
        for(i = 0; i < getNumParams(tuple->table); i++)
        {
            //printf("freetuploop\n");
            if(tuple->params[i] != NULL)
            {   
                free(tuple->params[i]);
            }
        }
        free(tuple->params);
    }
    
    free(tuple);

    return;
}

void freeTupleList(node_t* list)
{
    node_t* previousNode = list;
    //printf("in free list\n");
    while(list != NULL)
    {
        //printf("freelistloop\n");
        previousNode = list;
        list = list->next;
        freeTuple((tuple_t*)(previousNode->element));
        free(previousNode);
    }
    //printf("end free list\n");
    return;
}

